﻿@{
    ViewBag.Title = "数据库表信息";
}

<h2>数据库表信息</h2>

@using (Html.BeginForm("GetTableInfo", "SQL", FormMethod.Post, new { id = "sqlform", @class = "form-horizontal" }))
{
    <div class="form-group">
        <label for="txtsqlConn" class="col-sm-3 control-label">数据库链接字符串</label>
        <div class="col-sm-9">
            <input type="text" class="form-control" id="txtsqlConn" placeholder="数据库链接字符串">
        </div>
    </div>
    <div class="form-group">
        <label for="txtsqlConn" class="col-sm-3 control-label">数据库表</label>
        <div class="col-sm-7">
            <select class="form-control" id="selTableName" disabled>
                <option>请先输入数据库链接后点击刷新按钮</option>
            </select>
        </div>
        <div class="col-sm-2" style="margin:10px 0;">
            <button type="button" style="width:100%;" onclick="GetTable()" class="btn btn-default">刷新</button>
        </div>
    </div>

    <div class="form-group">
        <div class="col-sm-offset-3 col-sm-9">
            <button type="button" onclick="GetList()" class="btn btn-default">确定</button>
        </div>
    </div>
}

<div>
    <div class="panel panel-default">
        <div class="panel-body table-responsive">
            <table class="table table-bordered table-striped">
                <thead>
                    <tr>
                        <th>字段名</th>
                        <th>自增长</th>
                        <th>主键</th>
                        <th>数据类型</th>
                        <th>占用字节数</th>
                        <th>精度</th>
                        <th>小数位数</th>
                        <th>是否可空</th>
                        <th>默认值</th>
                        <th>字段说明</th>
                    </tr>
                </thead>
                <tbody id="templateContent"></tbody>
            </table>
        </div>
    </div>
</div>


<script id="datas" type="text/html">
    {{each list as values i}}
    <tr>
        <td>{{values["ColumnName"]}}</td>
        <td>{{values["IsIdentity"]}}</td>
        <td>{{values["PrimaryKey"]}}</td>
        <td>{{values["DataType"]}}</td>
        <td>{{values["TakeBytes"]}}</td>
        <td>{{values["DataPrecision"]}}</td>
        <td>{{values["DecimalScale"]}}</td>
        <td>{{values["IsAllowNull"]}}</td>
        <td>{{values["DefaultValue"]}}</td>
        <td>{{values["ColumnExplain"]}}</td>
    </tr>
    {{/each}}
</script>

<script type="text/javascript">
    var conn = "";
    function GetTable() {
        conn = $("#txtsqlConn").val();
        if (conn == null || conn.length == 0) {
            $("#selTableName").append('<option>请输入有效的数据库链接后点击刷新按钮</option>');
            $("#selTableName").attr("disabled", true);
        }

        $.post('/SQL/GetTable', { connStr: $("#txtsqlConn").val() }, function (datas) {
            $("#selTableName").empty();
            if (datas.Status) {
                if (datas.Data.length > 0) {
                    $("#selTableName").attr("disabled", false);
                }
                else {
                    $("#selTableName").append('<option>请输入有效的数据库链接后点击刷新按钮</option>');
                    $("#selTableName").attr("disabled", true);
                }
                $.each(datas.Data, function (index, obj) {
                    $("#selTableName").append('<option value="' + obj.id + '">' + obj.name + '</option>');
                })
            }
            else {
                $("#selTableName").append('<option>请输入有效的数据库链接后点击刷新按钮</option>');
                $("#selTableName").attr("disabled", true);
            }
        })
    }
    function GetList() {
        var tableId = $("#selTableName").val();
        if (tableId > 0) {
            $("#templateContent").html('');
            $.post($("#sqlform").attr("action"), { connStr: conn, tableId: tableId }, function (datas) {
                var html = template('datas', { list: datas.Data });
                $("#templateContent").append(html);
            });
        }
        else {

        }
    }
</script>